package com.twu.biblioteca.view.console.resolver;

import com.twu.biblioteca.entity.Movie;
import com.twu.biblioteca.enums.MapKey;
import com.twu.biblioteca.view.console.ViewResolver;

import java.util.List;
import java.util.Map;

public class MovieListViewResolver implements ViewResolver {
  public static final String HEADER =
      "-----------------------------------------------------------------------\n"
          + "|  Title                                 Director       Year    Rate  |\n"
          + "-----------------------------------------------------------------------\n";
  public static final String END_LIE =
      "-----------------------------------------------------------------------\n";

  private static final String BODY =
      "%d. %-38s%-15s%-9d%-2d\n";

  public String getView(Map<MapKey, Object> model) {
    List<Movie> bookList = (List<Movie>) model.get(MapKey.Movies);
    return HEADER + getBody(bookList) + END_LIE;
  }

  private String getBody(List<Movie> movieList) {
    String body = "";
    int index = 1;
    for (Movie m : movieList) {
      body += String.format(BODY, index, m.getTitle(), m.getDirector(), m.getYear(), m.getRate());
      index += 1;
    }
    return body;
  }
}
